import Vuex from 'vuex'
import Vue from 'vue'
import * as api from './api'

Vue.use(Vuex)

export const store = new Vuex.Store({
         state: {
           adminItems: [],
           count: 0, // 列表数量
           uptoken: '',
           uploadcfg: {},
           total: {},
           editor: {},
           isMarkdownEditor: false,
           curentPath: '', // 当前url路径，触发导航栏
           versions: [], // 版本列表
           grades: [], // 年级列表
           tags: [], // 标签列表
         },

         mutations: {
           SET_ITEM: (state, obj) => {
             state[obj.key] = obj.val;
           },
           DEL_ITEM: (state, obj) => {
             state[obj.key].splice(obj.index, 1);
           },
           ADD_ITEM: (state, obj) => {
             state[obj.key].unshift(obj.val);
           },
           UPDATE_ARRAY_ITEM: (state, obj) => {
             let array = state[obj.key];
             for (let item of array) {
               if (item._id === obj.id) {
                 item = obj.val;
                 break;
               }
             }
           }
         },

         actions: {
           LINK_CURENT_MENU: ({ commit, state }, req) => {
             commit('SET_ITEM', { key: 'curentPath', val: req.data });
           },
           FETCH_ADMIN_ITEMS: ({ commit, state }, req) => {
             return api
               ._get(req)
               .then(items => {
                 req.cb && req.cb(items.data.data);
                 commit('SET_ITEM', {
                   key: 'adminItems',
                   val: items.data.data.list
                 });
                 commit('SET_ITEM', {
                   key: 'count',
                   val: items.data.data.count
                 });
               })
               .catch(err => {
                 console.log(err);
                 req.msg && req.msg.error('error');
               });
           },
           FETCH_ADMIN_VERSION: ({ commit, state }, req) => {
            return api
              ._get(req)
              .then(items => {
                req.cb && req.cb(items.data.data);
                commit('SET_ITEM', {
                  key: req.key,
                  val: items.data.data.list
                });
              })
              .catch(err => {
                console.log(err);
                req.msg.error('error');
              });
          },
          ADD_ADMIN_ITEM: ({ commit, state }, req) => {
            api
              ._post(req)
              .then(result => {
                req.cb && req.cb(req.type);
                if (result.status === 200) {
                  req.msg.success('success');
                } else {
                  req.msg.error(result.data.msg || 'error');
                }
              })
              .catch(err => {
                console.log(err);
                req.msg.error('error');
              });
          },
           ADD_ADMIN_VERSION: ({ commit, state }, req) => {
             api
               ._post(req)
               .then(result => {
                 req.cb && req.cb(req.type);
                 if (result.status === 200) {
                   req.msg.success('success');
                 } else {
                   req.msg.error(result.data.msg || 'error');
                 }
               })
               .catch(err => {
                 console.log(err);
                 req.msg.error('error');
               });
           },
           UPDATE_ADMIN_ITEM: ({ commit, state }, req) => {
             api
               ._put(req)
               .then(result => {
                 req.cb && req.cb(result);
                 if (result.status === 200) {
                   req.msg.success('success');
                 } else {
                   req.msg.error('error');
                 }
               })
               .catch(err => {
                 console.log(err);
                 req.msg.error('error');
               });
           },
           PUBLISH_ADMIN_ITEM: ({ commit, state }, req) => {
             api
               ._put(req)
               .then(result => {
                 req.cb && req.cb(result);
                 if (result.status === 200) {
                   req.msg.success('success');
                 } else {
                   req.msg.error('error');
                 }
               })
               .catch(err => {
                 req.msg.error('error');
                 console.log(err);
               });
           },
           DELETE_ADMIN_ITEM: ({ commit, state }, req) => {
             api
               ._delete(req)
               .then(result => {
                 req.cb && req.cb(result);
                 if (result.status === 200) {
                   req.msg.success('success');
                 } else {
                   req.msg.error('error');
                 }
               })
               .catch(err => {
                 req.msg.error('error');
                 console.log(err);
               });
           }
         }
       });


